/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.infinityk.tangentgenerator;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.swing.JTextField;
import javax.swing.TransferHandler;

/**
 *
 * @author Asier
 */
public class FileDropTransferHandler extends TransferHandler{
    private MainFrame father;

    public FileDropTransferHandler(MainFrame father) {
        this.father = father;
    }


    @Override
    public boolean canImport(TransferSupport support) {
        if (support.isDataFlavorSupported(DataFlavor.javaFileListFlavor)){
            support.setDropAction(COPY);
            return true;
        }
        return false;
    }

    @Override
    public boolean importData(TransferSupport support) {
       if (support.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
            try{
                List<File> files = (List<File>) support.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
                if(! files.isEmpty()){
                    JTextField label = (JTextField)support.getComponent();
                    File f = files.get(0);
                    String path = f.getAbsolutePath();
                    label.setText( path );
                    father.calculateOutputPath(path);
                }
                return true;
            }catch(UnsupportedFlavorException e ){ e.printStackTrace();
            }catch(IOException e) {e.printStackTrace();}
        }
        return false;
    }
}
